DotNet Example: Using a Private Assembly

Description

Download and unzip the attached assembly, which corresponds to the C# source code in the first snippet. Paste the Xbasic code in the second snippet into an Alpha Anywhere script, edit the path to the location of the downloaded DLL, and run the script.

Discussion

The C# source code for the .NET class being used. Compile the example below into a DLL named "MyCSLib.dll" or download the DLL for the source below:

using System;
using System.Text;
 
namespace MyCSLib
{
    public class MyCSClass
    {
        public String MyProperty
        {
            get
            {
                return "Hello from MyCSClass";
            }
            set
            {
            }
        }
 
        public String DoSomething()
        {
            return "MyCSClass did something";
        }
    }
}

The Xbasic code below loads the MyCSLib.dll (code shown above) and registers classes available in the DLL. If class registration succeeds, it creates a MyCSClass object and performs several operations using the class:

dim Sv as DotNet::Services
dim assy as DotNet::AssemblyReference

'Update assy.FileName to use the location where the 
'MyCSLib.dll file is located on your system:
assy.FileName="c:\path\to\MyCSLib.dll"

if .not. file.exists(assy.FileName)
    ui_msg_box("can't find file",assy.FileName)
    end 
end if

rc = sv.RegisterClass("My","Class","MyCSLib.MyCSClass",assy)

if rc then
    dim cl as My::Class
    dim result as c
    result=cl.DoSomething()
    result= result + crlf() + cl.MyProperty
    showvar(result)
else
    ui_msg_box("MyCSLib Demo","MyCSClass class not registered")
    'Note: if you get here, try closing and restarting Alpha Anywhere
    'It usually means a previous registration failed
    'It can also mean that you put a downloaded file in a protected spot, such as C:\
    'You can right-click on the DLL file from Windows Explorer and unblock it.
end if

If the custom .NET class is successfully registered, a dialog will be shown with the following information:

MyCSClass did something
Hello from MyCSClass

See Also